package com.betty.cscams.service.impl;

import java.util.Date;
import java.util.List;

import com.betty.cscams.common.page.MybatisPageHelper;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.common.page.PageResult;
import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.dao.BusTermMapper;
import com.betty.cscams.dao.SysDeptMapper;
import com.betty.cscams.dao.SysUserMapper;
import com.betty.cscams.entity.*;
import com.betty.cscams.util.EasyPoiUtil;
import com.betty.cscams.util.SecurityUtils;
import com.betty.cscams.vo.ScoreQueryBean;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.betty.cscams.dao.BusScoreMapper;
import com.betty.cscams.service.BusScoreService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;

/**
 * ---------------------------
 *  (BusScoreServiceImpl)         
 * ---------------------------
 * 时间：  2022-04-03 12:19:27
 * ---------------------------
 */
@Service
public class BusScoreServiceImpl implements BusScoreService {

	@Autowired
	private BusScoreMapper busScoreMapper;
	@Autowired
	private BusTermMapper busTermMapper;
	@Autowired
	private SysDeptMapper sysDeptMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	public int save(BusScore record) {
		record.setLastUpdateBy(SecurityUtils.getUsername());
		record.setLastUpdateTime(new Date());
		if(record.getId() == null || record.getId() == 0) {
			record.setCreateBy(SecurityUtils.getUsername());
			record.setCreateTime(new Date());
			record.setStatus(SysConstants.REVIEW_STATUS_1);
			return busScoreMapper.add(record);
		}
		return busScoreMapper.update(record);
	}

	@Override
	public int delete(BusScore record) {
		return busScoreMapper.delete(record.getId());
	}

	@Override
	public int delete(List<BusScore> records) {
		for(BusScore record:records) {
			delete(record);
		}
		return 1;
	}

	@Override
	public BusScore findById(Long id) {
		return busScoreMapper.findById(id);
	}

	@Override
	public PageResult findPage(PageRequest pageRequest) {
		Object  subjectId = pageRequest.getParam("subjectId").toString();
		Object  subjectType = pageRequest.getParam("subjectType").toString();
		Object name = pageRequest.getParam("name");
		Object  deptId = pageRequest.getParam("deptId").toString();
		Object  termId = pageRequest.getParam("termId").toString();
		Object  status = pageRequest.getParam("status").toString();
		String student = "";
		SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());
		if("student".equals(sysUser.getType())){
			student = sysUser.getName();
		}
		String userType = sysUser.getType();

		return MybatisPageHelper.findPage(pageRequest, busScoreMapper,"findPage",subjectId,subjectType,name,deptId,termId,student,status,userType);

	}

	@Override
	public boolean findIsExit(BusScore record) {
		if(busScoreMapper.findIsExit(record) > 0){
			return true;
		}
		return false;
	}

	@Override
	public void deleteByEntity(BusScore busScore) {
		busScoreMapper.deleteByEntity(busScore);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void calculationSports(Long termId, Long deptId,Integer subjectType) {
		BusScore busScore = new BusScore();
		busScore.setTermId(termId);
		busScore.setSubjectId(Long.valueOf(0-subjectType));
		busScoreMapper.deleteByEntity(busScore);
		busScoreMapper.calculationTotal(termId,deptId,subjectType);
		List<BusScore> list = busScoreMapper.selectOrder(termId,deptId,0-subjectType);
		for (BusScore record:list) {
			busScoreMapper.calculationOrder(record);
		}
	}

	@Override
	public void calculationReset(Long termId, Long deptId,Integer subjectType) {
		BusScore busScore = new BusScore();
		busScore.setTermId(termId);
		busScore.setSubjectId(Long.valueOf(0-subjectType));
		busScoreMapper.deleteByEntity(busScore);
	}

	@Override
	public void exportSport(ScoreQueryBean params, HttpServletResponse response) {
		SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());
		BusTerm busTerm =busTermMapper.findById(params.getTermId());
		SysDept sysDept = sysDeptMapper.selectByPrimaryKey(params.getDeptId());
		params.setUserId(sysUser.getId());
		params.setUserType(sysUser.getType());
		List<BusScore> list = busScoreMapper.findAllByParams(params);
		String title = sysDept.getName()+"体育成绩表";
		String sheetName = "体育";
		String fileName = sysDept.getName()+"体育成绩表.xlsx";
		if(params.getSubjectType() == 1){
			 title = sysDept.getName()+"智育成绩表";
			 sheetName = "智育";
			 fileName = sysDept.getName()+"智育成绩表.xlsx";
		}
		EasyPoiUtil.exportExcel(list,title,sheetName,BusScore.class,fileName,true,response);
	}

	@Override
	public PageResult findPageTo(PageRequest pageRequest) {
		Object  subjectId = pageRequest.getParam("subjectId").toString();
		Object  subjectType = pageRequest.getParam("subjectType").toString();
		Object name = pageRequest.getParam("name");
		Object  deptId = pageRequest.getParam("deptId").toString();
		Object  termId = pageRequest.getParam("termId").toString();
		Object  status = pageRequest.getParam("status").toString();
		String student = "";
		SysUser sysUser = sysUserMapper.findByName(SecurityUtils.getUsername());
		if("student".equals(sysUser.getType())){
			student = sysUser.getName();
		}

		return MybatisPageHelper.findPage(pageRequest, busScoreMapper,"findPageTo",subjectId,subjectType,name,deptId,termId,student,status,sysUser.getId());

	}
}
